<?php
class agentController extends RPHP_Controller
{

    //权限前缀
    protected $_pvprefix = "agent";

/*----商品--------------------*/
    //商品列表
    public function goodslist()
    {

        $priv = array($this->_pvprefix . "_ls");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            if ($_GET['stat'] != "") {
                $stat = addslashes(trim($_GET['stat']));
            }

            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }

            }

            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }

            $this->view->data = $model->getGoods($stat, $para, $page);

            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }

            //新增权限
            $this->view->pv['add'] = $model->checkPriv($this->_pvprefix . "_ad", false, false);
            //编辑权限
            $this->view->pv['edit'] = $model->checkPriv($this->_pvprefix . "_ed", false, false);

            $this->display();

        }

    }

    //新增商品
    public function add()
    {
        $priv = array($this->_pvprefix . "_ad");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            $this->view->auth = $auth;

            $this->display();
        }

    }

    //编辑商品
    public function edit()
    {
        $priv = array($this->_pvprefix . "_ed");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $goods_id = intval($_GET['goods_id']);
            if ($goods_id > 0) {
                $data = $model->getSingleGoods($goods_id);
            }

            if ($data) {
                $this->view->auth = $auth;

                $this->view->data = $data;
                $this->display();

            }

        }

    }

    //保存商品
    public function savegoods()
    {
        $priv = array($this->_pvprefix . "_ls");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->code = 900;
            // 新增
            if ($_POST['act'] == "add" && $model->checkPriv($this->_pvprefix . "_ad", false, false)) {

                $this->view->success = "新增商品成功";
                $this->view->link    = array(
                    array('name' => '继续新增商品', 'url' => '/agent/add'),
                    array('name' => '返回商品列表', 'url' => '/agent/goodslist'),
                );
                $this->view->code = $model->saveGoods($_POST);

            }
            // 编辑
            elseif ($_POST['act'] == "edit" && $model->checkPriv($this->_pvprefix . "_ed", false, false)) {

                $this->view->success = "编辑商品成功";
                $this->view->link    = array(
                    array('name' => '返回来源页面', 'url' => $_POST['ref']),
                    array('name' => '返回商品列表', 'url' => '/agent/goodslist'),
                );
                $this->view->code = $model->saveGoods($_POST, $_POST['goods_id']);

            }

            $this->displaydo();

        }
    }

/*----升级设置-----------*/

    public function setting()
    {
        $priv = array($this->_pvprefix . "_up");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            $this->view->data = $model->getSetting();
            // print_r($this->view->data);

            $this->display();

        }
    }

    //新增
    public function settingadd()
    {
        $priv = array($this->_pvprefix . "_up");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            $this->view->auth = $auth;

            $this->display();
        }

    }

    //编辑商品
    public function settingedit()
    {
        $priv = array($this->_pvprefix . "_up");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $id = intval($_GET['id']);
            if ($id > 0) {
                $data = $model->getSingleSetting($id);
            }

            if ($data) {
                $this->view->auth = $auth;
                $this->view->data = $data;
                $this->display();

            }

        }

    }

    //保存
    public function savesetting()
    {
        $priv = array($this->_pvprefix . "_up");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->code = 900;
            // 新增
            if ($_POST['act'] == "add") {

                $this->view->success = "新增设置成功";
                $this->view->link    = array(
                    array('name' => '继续新增设置', 'url' => '/agent/settingadd'),
                    array('name' => '返回升级设置', 'url' => '/agent/setting'),
                );
                $this->view->code = $model->saveSetting($_POST);

            }
            // 编辑
            elseif ($_POST['act'] == "edit") {

                $this->view->success = "编辑设置成功";
                $this->view->link    = array(
                    array('name' => '返回商品列表', 'url' => '/agent/setting'),
                );
                $this->view->code = $model->saveSetting($_POST, $_POST['id']);

            }

            $this->displaydo();

        }
    }

/*充值库存*/

    public function stock()
    {
        $priv = array($this->_pvprefix . "_st");

        $m    = $this->model;
        $auth = $m->checkPriv($priv);
        if ($auth) {
            
            $this->view->auth = $auth;

            $uid = intval($_GET['uid']);
            if ($uid > 0) {
                $this->view->user = $m->getUser($uid);
            }

            if ($this->view->user) {
                $this->view->data = $m->getAllGoods();

                $this->view->stock = $m->getStock($uid);
            }

            $this->display();

        }
    }

    public function ajax()
    {

        $data['code'] = 901;

        $priv = array($this->_pvprefix . "_st");

        $m    = $this->model;
        $auth = $m->checkPriv($priv,false,false);
        if ($auth) {
            
            if($_POST['act']=="recharge")
            {
                $data = $m->saveRecharge($_POST);
            }

        }

        echo json_encode($data);
    }

}
